package com.soonfor.evaluate.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.estronger.shopmanage.view.RoundImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import com.estronger.shopmanage.R;
import com.soonfor.evaluate.tools.ImageUtils;
import com.soonfor.evaluate.bean.EvaluateRankingBean;

/**
 * 作者：DC-DingYG on 2018-10-17 13:57
 * 邮箱：dingyg012655@126.com
 */
public class MeEvaluateRankView extends LinearLayout {
    private Context mContext;
    @BindView(R.id.left_avatar)
    RoundImageView leftAvatar;
    @BindView(R.id.tv_left_ranking)
    TextView tvLeftRanking;
    @BindView(R.id.tvfLeftGoodRate)
    TextView tvfLeftGoodRate;
    @BindView(R.id.tvfLeftGrade)
    TextView tvfLeftGrade;
    @BindView(R.id.me_avatar)
    RoundImageView middleAvatar;
    @BindView(R.id.tv_me_ranking)
    TextView tvMiddleRanking;
    @BindView(R.id.tvfMiddleGoodRate)
    TextView tvfMiddleGoodRate;
    @BindView(R.id.tvfMiddleGrade)
    TextView tvfMiddleGrade;
    @BindView(R.id.right_avatar)
    RoundImageView rightAvatar;
    @BindView(R.id.tv_right_ranking)
    TextView tvRightRanking;
    @BindView(R.id.tvfRightGoodRate)
    TextView tvfRightGoodRate;
    @BindView(R.id.tvfRightGrade)
    TextView tvfRightGrade;
    @BindView(R.id.rl_rank1)
    RelativeLayout rlRank1;
    @BindView(R.id.rl_rank2)
    RelativeLayout rlRank2;
    @BindView(R.id.rl_rank3)
    RelativeLayout rlRank3;
    @BindView(R.id.leftKuan)
    ImageView leftKuan;
    @BindView(R.id.middleKuan)
    ImageView middleKuan;
    @BindView(R.id.rightKuan)
    ImageView rightKuan;

    @BindView(R.id.tvfLeftName)
    TextView tvfLeftName;
    @BindView(R.id.tvfMiddleName)
    TextView tvfMiddleName;
    @BindView(R.id.tvfRightName)
    TextView tvfRightName;

    public MeEvaluateRankView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.me_evaluate_ranking, this);
        ButterKnife.bind(this, view);
    }

    //初始化
    public void initEvaluateRankingView(final Activity mContext, final List<EvaluateRankingBean> list, final int ranking) {
        this.mContext = mContext;
        if (list != null && list.size() > 0) {
            rlRank2.setVisibility(View.VISIBLE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (ranking - 1 >= 0) {
                                    rlRank1.setVisibility(VISIBLE);
                                    rlRank2.setVisibility(VISIBLE);
                                    tvLeftRanking.setText(getRanksort(list, ranking - 1));
                                    tvfLeftName.setText(list.get(ranking - 1).getFusername());
                                    tvfLeftGoodRate.setText(list.get(ranking - 1).getFallhighappraiserate() + "%");
                                    tvfLeftGrade.setText(list.get(ranking - 1).getFavgrtnvisitpoint() + "分");
                                    tvMiddleRanking.setText(getRanksort(list, ranking));
                                    tvfMiddleName.setText(list.get(ranking).getFusername());
                                    tvfMiddleGoodRate.setText(list.get(ranking).getFallhighappraiserate() + "%");
                                    tvfMiddleGrade.setText(list.get(ranking).getFavgrtnvisitpoint() + "分");
                                    ImageUtils.loadImage(mContext, list.get(ranking - 1).getFheadpic(), leftAvatar, R.drawable.user2);
                                    ImageUtils.loadImage(mContext, list.get(ranking).getFheadpic(), middleAvatar, R.drawable.user2);
                                    if (ranking + 1 < list.size()) {
                                        rlRank3.setVisibility(VISIBLE);
                                        tvRightRanking.setText(getRanksort(list, ranking + 1));
                                        tvfRightName.setText(list.get(ranking + 1).getFusername());
                                        tvfRightGoodRate.setText(list.get(ranking + 1).getFallhighappraiserate() + "%");
                                        tvfRightGrade.setText(list.get(ranking + 1).getFavgrtnvisitpoint() + "分");
                                        ImageUtils.loadImage(mContext, list.get(ranking + 1).getFheadpic(), rightAvatar, R.drawable.user2);
                                    } else {
                                        rlRank3.setVisibility(INVISIBLE);
                                    }
                                } else {
                                    rlRank1.setVisibility(VISIBLE);
                                    tvLeftRanking.setText(getRanksort(list, ranking));
                                    tvfLeftName.setText(list.get(ranking).getFusername());
                                    tvfLeftGoodRate.setText(list.get(ranking).getFallhighappraiserate() + "%");
                                    tvfLeftGrade.setText(list.get(ranking).getFavgrtnvisitpoint() + "分");
                                    ImageUtils.loadImage(mContext, list.get(ranking).getFheadpic(), leftAvatar, R.drawable.user2);
                                    if (ranking + 1 < list.size()) {
                                        rlRank2.setVisibility(VISIBLE);
                                        tvMiddleRanking.setText(getRanksort(list, ranking + 1));
                                        tvfMiddleName.setText(list.get(ranking + 1).getFusername());
                                        tvfMiddleGoodRate.setText(list.get(ranking + 1).getFallhighappraiserate() + "%");
                                        tvfMiddleGrade.setText(list.get(ranking + 1).getFavgrtnvisitpoint() + "分");
                                        ImageUtils.loadImage(mContext, list.get(ranking + 1).getFheadpic(), middleAvatar, R.drawable.user2);
                                        if (ranking + 2 < list.size()) {
                                            rlRank3.setVisibility(VISIBLE);
                                            tvRightRanking.setText(getRanksort(list, ranking + 2));
                                            tvfRightName.setText(list.get(ranking + 2).getFusername());
                                            tvfRightGoodRate.setText(list.get(ranking + 2).getFallhighappraiserate() + "%");
                                            tvfRightGrade.setText(list.get(ranking + 2).getFavgrtnvisitpoint() + "分");
                                            ImageUtils.loadImage(mContext, list.get(ranking + 2).getFheadpic(), rightAvatar, R.drawable.user2);
                                        } else {
                                            rlRank3.setVisibility(INVISIBLE);
                                        }
                                    } else {
                                        rlRank2.setVisibility(INVISIBLE);
                                        rlRank3.setVisibility(INVISIBLE);
                                    }
                                }
                                if(ranking>=0){ setKuan(list.size(), ranking); }
                            }catch (Exception e){
                                e.getMessage();
                            }
                        }
                    });
                }
            }).start();
        }
    }

    private void setKuan(int listsize, int ranking) {
        switch (ranking) {
            case 0:
                leftKuan.setVisibility(VISIBLE);
                middleKuan.setVisibility(INVISIBLE);
                rightKuan.setVisibility(INVISIBLE);
                break;
            case 1:
                leftKuan.setVisibility(INVISIBLE);
                middleKuan.setVisibility(VISIBLE);
                rightKuan.setVisibility(INVISIBLE);
                break;
            case 2:
                if(listsize>=3){
                    leftKuan.setVisibility(INVISIBLE);
                    middleKuan.setVisibility(VISIBLE);
                    rightKuan.setVisibility(INVISIBLE);
                }else {
                    leftKuan.setVisibility(INVISIBLE);
                    middleKuan.setVisibility(INVISIBLE);
                    rightKuan.setVisibility(VISIBLE);
                }
                break;
            default:
                leftKuan.setVisibility(INVISIBLE);
                middleKuan.setVisibility(VISIBLE);
                rightKuan.setVisibility(INVISIBLE);
                break;
        }
    }

    private String getRanksort(List<EvaluateRankingBean> list, int position) {
        if (!list.get(position).getFsno().equals("")) {
            return "第" + list.get(position).getFsno() + "名";
        } else
            return "";
    }

    ;

    public interface SetMainView {
        void callback(EvaluateRankingBean currtBean);
    }
}
